<?php
include_once 'application/application.php';

error_reporting(E_ALL);
ini_set('display_errors', '1');

class Page_cache {

    
    public $action;
    public $error;
    
    private $_script = '/usr/local/bin/rp-cache';


    public function __construct() {
        $this->action = Arr::getValue($_POST, 'action', false);
        /*
        DB::connect();
        if (!DB::state()) {
            $this->error = DB::getError();
        }*/
    }

    public function __destruct() {

    }



    private function isWin() {
        $path = Arr::getValue($_SERVER, 'PATH', false);
        $win = ($path && strpos(strtoupper($path),  'WINDOWS'));
        return $win;
    }

    
    public function execCache() {
        sleep(10);
        $J = new JSON();
        if (!Auth::isLogged()) return $J->error('not_logged');
        if ($this->isWin()) return $J->error('На Виндах не работает');
        
        $t = array();
        
        switch($this->action){
            case ACTIONS::CACHE_D:      exec($this->_script.' -d',$t);    break;
            case ACTIONS::CACHE_M:	exec($this->_script.' -m',$t);    break;
            case ACTIONS::CACHE_Y:	exec($this->_script.' -y',$t);    break;
            default: return $J->error('Неизвестный тип запроса');
        }
        
        $J->result = join("<br>\n", $t);
        return $J;
    }
    


    public function outPage() {
        $params = array();

        $this->printOut(includeFile(APP_PAGE_PARTS.'page_cache.php', $params));
    }




    public function printOut( $content = '' ) {

        include_once APP_PAGE_PARTS.'header.php';
        echo Navbar::getHorisontalBar();
        echo Auth::loginForm(true);

        $params = array(
            'leftmenu'  => Navbar::getVerticalBar('leftmenu'),
            'content'   => $content? $content : ''
        );
        include APP_PAGE_PARTS.'columns.php';

        include_once APP_PAGE_PARTS.'footer.php';
        die();
    }



}

$page = new Page_cache();

if ($page->action) {
    //if (in_array($page->action, array(ACTIONS::CACHE_D,ACTIONS::CACHE_M,ACTIONS::CACHE_Y)) ) 
    echo $page->execCache();
}
else  {
    if (!Auth::isLogged()) reLocate ('index.php');

    Navbar::$expanded = 'lm-sys-bot';
    Navbar::$active = 'cache_manual';
    $page->outPage();

}
?>